// 引入在线jquery
// var script=document.createElement("script");
// script.type="text/js";
// script.src="http://apps.bdimg.com/libs/jquery/2.1.1/jquery.min.js";
// document.getElementsByTagName('head')[0].appendChild(script); 

/*
jq foreach 遍历数组和对象
$.each(array, function(index, val) {
    console.log( index, val, this );
});
$.each(obj, function(key, val) {
    console.log(key, val, this );
});
*/

//ajax请求发送
function send(url, type, data, contentType) {
    data = isBlank(data) ? null : JSON.stringify(data);
    contentType = isBlank(contentType) ? "application/json" : contentType;

    var res = null;
    $.ajax({
        headers: {
            'x-app-token': isBlank(currentUser) ? '' : currentUser.token
        },
        url: url,
        contentType: contentType,
        async: false,
        type: type,
        data: data,
        success: function(result) {
            res = result;
        },
        error: function(e) {
            writeLog("服务请求失败:" + JSON.stringify(e));
        }
    });
    return res;
}

//字符串判空
function isBlank(str) {
    if(undefined == str || null == str || 'null' == str || '' == str) {
        return true;
    }
    return false;
}

//返回结果判断
function isSuccess(res) {
    if(!isBlank(res) && res.success) {
        return true;
    }

    writeLog('返回结果不通过', JSON.stringify(res));
    return false;
}

//写日志
function writeLog(log, param) {
    if($('#logDiv').css('display') == 'none') {
        $('#logDiv').css('display', '');
    }

    if(isBlank(param)) {
        $('#logDiv').append('<li style=font-size:15px>' + log + '</li>');
    } else {
        $('#logDiv').append('<li style=font-size:15px>' + log + '[' + JSON.stringify(param) + ']</li>');
    }

}

//获取指定区间范围随机数，包括lowerValue和upperValue
function randomFrom(lowerValue, upperValue) {
    return Math.floor(Math.random() * (upperValue - lowerValue + 1) + lowerValue);
}

/**
 * 按字段名称直接生成form表单,默认表单样式.myForm,选择器不传入默认追加到body
 * @param {Object} objData		表单对象数据
 * @param {Object} objColumn	自定义对应中文名称和字段值
 * @param {Object} objClassName	类名称
 * @param {Object} selector		选择器
 * 
 * 调用列子:
 *  var obj = {a:1,b:2,c:3,d:4,e:5,f:6,g:7};
    var config = [{ColumnName:'描述a',DataId:'a'},{ColumnName:'描述b',DataId:'b'},{ColumnName:'描述c',DataId:'c'},{ColumnName:'描述d',DataId:'d'},{ColumnName:'描述e',DataId:'e'},{ColumnName:'描述f',DataId:'f'},{ColumnName:'描述g',DataId:'g'}]
    makeForm(obj,config)
    */
function makeForm(objData, objColumn, objClassName, selector) {
    //默认样式
    objClassName = isBlank(objClassName) ? "myFormData" : objClassName;

    var form = document.createElement("form");
    if(isBlank(selector)) {
        $('body').append(form);
    } else {
        selector.html(form);
    }

    var html = "";
    if(!isBlank(objColumn)) {

        $.each(objColumn, function(index, o) {
            html += '<div>';
            html += '<label>' + o.ColumnName + '</label>:&nbsp;&nbsp;';
            html += '<input type=text name=' + o.DataId + ' value=' + objData[o.DataId] + '>';
            html += '</div>';
        });
    } else {

        for(var e in objData) {
            html += '<div>';
            html += '<label>' + e + '</label>:&nbsp;&nbsp;';
            html += '<input type=text name=' + e + ' value=' + objData[e] + '>';
            html += '</div>';
        }
    }

    if(!isBlank(objClassName)) {
        $(form).attr("class", objClassName);
    }
    $(form).html(html);
}

/**
 * 按字段名称直接生成行数据
 * @param {Object} objColumn
 * @param {Object} objData
 * @param {Object} objClassName
 * @param {Object} selector
 * 
 *  调用列子:
 *  var obj = {a:1,b:2,c:3,d:4,e:5,f:6,g:7};
    var config = [{ColumnName:'描述a',DataId:'a'},{ColumnName:'描述b',DataId:'b'},{ColumnName:'描述c',DataId:'c'},{ColumnName:'描述d',DataId:'d'},{ColumnName:'描述e',DataId:'e'},{ColumnName:'描述f',DataId:'f'},{ColumnName:'描述g',DataId:'g'}]
    makeRow(obj,config)
    */
function makeRow(objData, objColumn, objClassName, selector) {
    //默认样式
    objClassName = isBlank(objClassName) ? "myDivData" : objClassName;

    var div = document.createElement("div");
    div.style = "margin-top:1%";
    div.attributes.className = "myDivData";

    if(isBlank(selector)) {
        $('body').append(div);
    } else {
        selector.html(div);
    }

    var html = "";
    if(isBlank(objColumn)) {
        for(var e in objData) {
            html += '<div>';
            html += '<label>' + e + ':&nbsp;</label>';
            html += '<label>' + objData[e] + '</label>';
            html += '</div>';
        }
    } else {

        $.each(objColumn, function(index, o) {
            html += '<div>';
            html += '<label>' + o.ColumnName + ':&nbsp;</label>';
            html += '<label>' + objData[o.DataId] + '</label>';
            html += '</div>';
        });
    }

    if(!isBlank(objClassName)) {
        $(div).attr("class", objClassName);
    }
    $(div).html(html);
}

/**
 * 生成下拉框 格式[{value:'value1',text:'text1'}]
 * @param {Object} objData
 * @param {Object} selector
 */
function makeSelect(objData, selector) {
    var sel = selector;

    if(isBlank(selector)) {
        sel = document.createElement("select");
        $('body').append(sel);
    }

    var html = "<option value=''>请选择</option>";
    $.each(objData, function(index, o) {
        html += '<option value=' + o.value + ' data= ' + JSON.stringify(o.target) + '>' + o.text + '</option>';
    });
    $(sel).html(html);
}

//休眠函数
function sleep(numberMillis) {    
    var now = new Date();    
    var exitTime = now.getTime() + numberMillis;   
    while (true) { 
        now = new Date();       
        if (now.getTime() > exitTime) {
            return;
        }
    } 
}